<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Vue中的动画封装</title>
	<script type="text/javascript" src="../js/vue.js"></script>
	<style type="text/css">
		.v-enter, .v-leave-to {
			opacity: 0;
		}
		.v-enter-active, .v-leave-active {
			transition: opacity 1s
		}
	</style>
</head>
<body>
	<div id="app">
		<fade :show="show">
			<div>hello world</div>
		</fade>
		<fade :show="show">
			<h1>hello world</h1>
		</fade>
		<button @click="change">Change</button>
	</div>
	<script type="text/javascript">

		Vue.component('fade', {
			props: ['show'],
			template: `
				<transition @before-enter="handleBeforeEnter"
				  @enter="handlerEnter">
					<slot v-if="show"></slot>
				</transition>
			`,
			methods: {
				handleBeforeEnter: function(el) {
					el.style.color = 'red'
				},
				handlerEnter: function(el, done) {
					setTimeout(() => {
						el.style.color = 'green'
						done()
					}, 2000)
				}
			}
		})

		var vm = new Vue({
			el: '#app',
			data: {
				show: false
			},
			methods: {
				change: function() {
					this.show = !this.show
				}
			}
		}) 
	</script>
</body>
</html>